package rbac

import (
	"github.com/gin-gonic/gin"
	"store/libs/helper"
	"store/libs/log"
	"store/models"
	"store/models/base"
)

const (
	TABLE_ADMIN_ACCESS = "admin_access"
)

type AdminAccess struct {
	RightId    uint64 `json:"right_id" xml:"right_id"`
	IsOn       uint8  `json:"is_on" xml:"is_on"`
	Version    uint64 `json:"version" xml:"version"`
	AddTime    string `json:"add_time" xml:"add_time"`
	UpdateTime uint64 `json:"update_time" xml:"update_time"`
	Id         uint64 `json:"id" xml:"id"`
	RoleId     uint64 `json:"role_id" xml:"role_id"`
}

/**
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminAccessMap2Model(data map[string]string) AdminAccess {
	return AdminAccess{
		RightId:    uint64(helper.ToInt(data["right_id"])),
		IsOn:       uint8(helper.ToInt(data["is_on"])),
		Version:    uint64(helper.ToInt(data["version"])),
		AddTime:    data["add_time"],
		UpdateTime: uint64(helper.ToInt(data["update_time"])),
		Id:         uint64(helper.ToInt(data["id"])),
		RoleId:     uint64(helper.ToInt(data["role_id"])),
	}
}

/**
 * 获取所有启用角色权限
 */
func AllEnableAccess(ctx *gin.Context) map[uint64][]uint64 {
	accessList := map[uint64][]uint64{}

	rows, err := models.Orm.Table(TABLE_ADMIN_ACCESS).Where("is_on = 1").Rows()
	if err != nil {
		log.Error(ctx, err.Error())
		return accessList
	}

	list := base.Rows2MapList(ctx, rows)
	if len(list) < 1 {
		return accessList
	}

	for _, m := range list {
		access := AdminAccessMap2Model(m)
		if _, exists := accessList[access.RoleId]; !exists {
			accessList[access.RoleId] = []uint64{}
		}
		accessList[access.RoleId] = append(accessList[access.RoleId], access.RightId)
	}

	return accessList
}

/**restful新增数据
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminAccessCreate(ctx *gin.Context, info map[string]interface{}) uint64 {
	id := base.NewCommonModel(ctx).Create(TABLE_ADMIN_ACCESS, info)
	return uint64(id)
}

/**restful删除，数据库表中必须有id字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminAccessDelete(ctx *gin.Context, id uint64) int64 {
	return base.NewCommonModel(ctx).Delete(TABLE_ADMIN_ACCESS, helper.ToString(id))
}

/**restful更新(带乐观锁)，数据库表中必须有id,version字段
uint64uint64uint64 * @email jhq0113@163.com
 * generated by gii
*/
func AdminAccessUpdate(ctx *gin.Context, id uint64, version uint64, set map[string]interface{}) int64 {
	return base.NewCommonModel(ctx).UpdateWithLock(TABLE_ADMIN_ACCESS, helper.ToString(id), version, set)
}

/**restful禁用启用，数据库表中必须有is_on字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminAccessUpOrDown(ctx *gin.Context, id uint64, value uint8) int64 {
	return base.NewCommonModel(ctx).UpOrDown(TABLE_ADMIN_ACCESS, helper.ToString(id), value)
}

/**restful详情，数据库表中必须有id字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminAccessInfo(ctx *gin.Context, where map[string]interface{}) AdminAccess {
	info := base.NewCommonModel(ctx).Info(TABLE_ADMIN_ACCESS, where)
	if info != nil {
		return AdminAccessMap2Model(info)
	}
	return AdminAccess{}
}

/**restful列表分页检索
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminAccessIndex(ctx *gin.Context, page int64, pageSize int64, where map[string]interface{}, orders []string) map[string]interface{} {
	result := base.NewCommonModel(ctx).Index(TABLE_ADMIN_ACCESS, page, pageSize, where, orders)

	if len(result["list"].([]map[string]string)) > 0 {
		list := []AdminAccess{}
		for _, data := range result["list"].([]map[string]string) {
			list = append(list, AdminAccessMap2Model(data))
		}
		result["list"] = list
	}

	return result
}
